package com.x.crm.dao;

import com.x.crm.model.Insurance;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class InsuranceDao extends BaseDao {
    private final static String baseQuerySql = "SELECT * FROM CRM_INSURANCE T1 WHERE T1.INSURANCE_ID > 0 ";
    public List<Insurance> selectInsuranceList(Insurance insurance) throws Exception {
        StringBuilder sql = new StringBuilder(baseQuerySql);
        List<Object> params = new ArrayList<>();
        if (this.isNotNull(insurance.getMarketId())) {
            sql.append("AND T1.MARKET_ID = ? ");
            params.add(insurance.getMarketId());
        }
        if (this.isNotNull(insurance.getInsuranceName())) {
            sql.append("AND T1.INSURANCE_NAME  = ? ");
            params.add(insurance.getInsuranceName());
        }
        if (this.isNotNull(insurance.getInsuranceId())) {
            sql.append("AND T1.INSURANCE_ID  = ? ");
            params.add(insurance.getInsuranceId());
        }
        if (this.isNotNull(insurance.getShopId())) {
            sql.append("AND T1.SHOP_ID  = ? ");
            params.add(insurance.getShopId());
        }
        sql.append(" ORDER BY T1.INSURANCE_CREATE_TIME DESC ");
        return this.queryForList(sql.toString(), Insurance.class, params, insurance);
    }

    /**
     * 查询条数
     * @param Insurance
     * @return
     * @throws Exception
     */
    private final static  String  selectInsuranceCountSql = "SELECT COUNT(T1.INSURANCE_ID) FROM  CRM_INSURANCE T1 WHERE 1=1 ";
    public int selectInsuranceNum(Insurance  insurance) throws Exception{
        StringBuilder sql = new StringBuilder(selectInsuranceCountSql);
        List<Object> params = new ArrayList<Object>();
        if (this.isNotNull(insurance.getMarketId())) {
            sql.append("AND T1.MARKET_ID = ? ");
            params.add(insurance.getMarketId());
        }
        if (this.isNotNull(insurance.getInsuranceName())) {
            sql.append("AND T1.INSURANCE_NAME = ? ");
            params.add(insurance.getInsuranceName());
        }
        if (this.isNotNull(insurance.getShopId())) {
            sql.append("AND T1.SHOP_ID  = ? ");
            params.add(insurance.getShopId());
        }
        return this.queryForInt(sql.toString(), params);
    }

}
